package com.drugmanage.service.impl;

import com.drugmanage.constant.DmConstant;
import com.drugmanage.dao.ModuleDao;
import com.drugmanage.dao.PermissionDao;
import com.drugmanage.dao.RoleDao;
import com.drugmanage.dao.UserRoleDao;
import com.drugmanage.entity.Permission;
import com.drugmanage.entity.Role;
import com.drugmanage.query.RoleQuery;
import com.drugmanage.service.IRoleService;
import com.drugmanage.utils.AssertUtil;
import com.drugmanage.utils.ValidateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleService implements IRoleService {

    @Resource
    private RoleDao roleDao;

    @Resource
    private PermissionDao permissionDao;

    @Resource
    private ModuleDao moduleDao;

    @Resource
    private UserRoleDao userRoleDao;

    public List<Role> quertAllRoles() {
        return roleDao.queryAllRoles();
    }


    public void saveRole(Role role) {
        AssertUtil.isTure(StringUtils.isBlank(role.getRoleName()), "角色不能为空!");
        AssertUtil.isTure(null != roleDao.queryRoleByRoleName(role.getRoleName()), "角色名不能重复!");
        role.setIsValid(true);
        role.setCreateDate(new Date());
        role.setUpdateDate(new Date());
        AssertUtil.isTure(roleDao.insert(role) < 1, DmConstant.OP_FAILED_MSG);

    }

    public void updateRole(Role role) {
        AssertUtil.isTure(StringUtils.isBlank(role.getRoleName()), "角色名称非空!!");
        Role temp = roleDao.queryById(role.getId());
        AssertUtil.isTure(null != temp && temp.getId() != role.getId(), "角色名称不能重复！");
        role.setUpdateDate(new Date());
        AssertUtil.isTure(roleDao.update(role) < 1, DmConstant.OP_FAILED_MSG);
    }

    public void deleteRoles(Integer roleId) {
        AssertUtil.isTure(null == roleId || roleId < 1, "请选择待删除记录!");
        int total = userRoleDao.queryUserRolesByRoleId(roleId);
        if (total > 0) {
            AssertUtil.isTure(userRoleDao.deleteUserRolesByRoleId(roleId) < total, DmConstant.OP_FAILED_MSG);
        }

        AssertUtil.isTure(roleDao.delete(roleId) < 1, DmConstant.OP_FAILED_MSG);
    }

    @Override
    public void addGrant(Integer roleId, Integer[] moduleIds) {
        AssertUtil.isTure(ValidateUtil.nullOrZeroInteger(roleId), "请选择角色");
        AssertUtil.isTure(roleDao.queryById(roleId) == null, "角色不存在");

        int total = permissionDao.queryPermissionsByRoleId(roleId);

        if(total>0){
            AssertUtil.isTure(permissionDao.deleteHistoryPermissionsByRoleId(roleId)<total, DmConstant.OP_FAILED_MSG);
        }

        if (null != moduleIds && moduleIds.length > 0) {
            for (Integer moduleId : moduleIds) {
                Permission permission = new Permission();
                permission.setRoleid(roleId);
                permission.setModuleid(moduleId);
                permission.setAclvalue(moduleDao.get(moduleId).getOptValue());
                permission.setCreatedate(new Date());
                permission.setUpdatedate(new Date());
                AssertUtil.isTure(permissionDao.save(permission) < 1, "授权失败");
            }
        }
    }

    @Override
    public List<Role> queryAllRoles() {
        return roleDao.queryAllRoles();
    }

    public Map<String, Object> queryForPage(RoleQuery roleQuery) {
        PageHelper.startPage(roleQuery.getPage(), roleQuery.getRows());
        List<Role> roles = roleDao.queryForPage(roleQuery);
        PageInfo<Role> pageInfo = new PageInfo<Role>(roles);
        Map<String, Object> map = new HashMap<>();
        map.put("total", pageInfo.getTotal());
        map.put("rows", pageInfo.getList());
        return map;
    }
}
